using System;
using System.Collections.Generic;
using System.Linq;

using Csla;

using EntModApp.EntServices.Business;
using EntModApp.EntServices.IoC;

using DemoApp.DataAccess;

namespace DemoApp.BusinessLayer 
{
  [Serializable]
  public partial class CslaSubjectEdit : 
    BusinessBase<CslaSubjectEdit>,
    ISubject
  {
    #region [ Business Properties ]

    public static PropertyInfo<Guid> SubjectIdPropertyInfo = RegisterProperty(new PropertyInfo<Guid>("SubjectId"));
    public Guid SubjectId 
    { 
      get
      {
        return GetProperty(SubjectIdPropertyInfo);
      }
      private set
      {
        LoadProperty(SubjectIdPropertyInfo, value);
      }
    }

    public static PropertyInfo<string> UrlPropertyInfo = RegisterProperty(new PropertyInfo<string>("Url"));
    public string Url 
    { 
      get
      {
        return GetProperty(UrlPropertyInfo);
      }
      set
      {
        LoadProperty(UrlPropertyInfo, value);
      }
    }

    public static PropertyInfo<string> TitlePropertyInfo = RegisterProperty(new PropertyInfo<string>("Title"));
    public string Title 
    { 
      get
      {
        return GetProperty(TitlePropertyInfo);
      }
      set
      {
        LoadProperty(TitlePropertyInfo, value);
      }
    }

    public static PropertyInfo<string> DescriptionPropertyInfo = RegisterProperty(new PropertyInfo<string>("Description"));
    public string Description 
    { 
      get
      {
        return GetProperty(DescriptionPropertyInfo);
      }
      set
      {
        LoadProperty(DescriptionPropertyInfo, value);
      }
    }

    public static PropertyInfo<string> ImageUrlPropertyInfo = RegisterProperty(new PropertyInfo<string>("ImageUrl"));
    public string ImageUrl 
    { 
      get
      {
        return GetProperty(ImageUrlPropertyInfo);
      }
      set
      {
        LoadProperty(ImageUrlPropertyInfo, value);
      }
    }

    public static PropertyInfo<DateTime> CreateDatePropertyInfo = RegisterProperty(new PropertyInfo<DateTime>("CreateDate"));
    public DateTime CreateDate 
    { 
      get
      {
        return GetProperty(CreateDatePropertyInfo);
      }
      set
      {
        LoadProperty(CreateDatePropertyInfo, value);
      }
    }

    public static PropertyInfo<DateTime> ModifyDatePropertyInfo = RegisterProperty(new PropertyInfo<DateTime>("ModifyDate"));
    public DateTime ModifyDate 
    { 
      get
      {
        return GetProperty(ModifyDatePropertyInfo);
      }
      set
      {
        LoadProperty(ModifyDatePropertyInfo, value);
      }
    }

    public static PropertyInfo<bool> IsDisabledPropertyInfo = RegisterProperty(new PropertyInfo<bool>("IsDisabled"));
    public bool IsDisabled 
    { 
      get
      {
        return GetProperty(IsDisabledPropertyInfo);
      }
      set
      {
        LoadProperty(IsDisabledPropertyInfo, value);
      }
    }

    public static PropertyInfo<CslaUserEdit> CreatorPropertyInfo = RegisterProperty(new PropertyInfo<CslaUserEdit>("Creator"));
    public CslaUserEdit Creator 
    { 
      get
      {
        return GetProperty(CreatorPropertyInfo);
      }
      private set
      {
        LoadProperty(CreatorPropertyInfo, value);
      }
    }

    public static PropertyInfo<Guid> CreatorIdPropertyInfo = RegisterProperty(new PropertyInfo<Guid>("CreatorId"));
    public Guid CreatorId 
    { 
      get
      {
        return GetProperty(CreatorIdPropertyInfo);
      }
      set
      {
        LoadProperty(CreatorIdPropertyInfo, value);
      }
    }

    public static PropertyInfo<CslaUserEditCollection> EditorsPropertyInfo = RegisterProperty(new PropertyInfo<CslaUserEditCollection>("Editors"));
    public CslaUserEditCollection Editors 
    { 
      get
      {
        return GetProperty(EditorsPropertyInfo);
      }
      private set
      {
        LoadProperty(EditorsPropertyInfo, value);
      }
    }

    public static PropertyInfo<CslaSubjectEdit> ParentSubjectPropertyInfo = RegisterProperty(new PropertyInfo<CslaSubjectEdit>("ParentSubject"));
    public CslaSubjectEdit ParentSubject 
    { 
      get
      {
        return GetProperty(ParentSubjectPropertyInfo);
      }
      private set
      {
        LoadProperty(ParentSubjectPropertyInfo, value);
      }
    }

    public static PropertyInfo<Guid?> ParentSubjectIdPropertyInfo = RegisterProperty(new PropertyInfo<Guid?>("ParentSubjectId"));
    public Guid? ParentSubjectId 
    { 
      get
      {
        return GetProperty(ParentSubjectIdPropertyInfo);
      }
      set
      {
        LoadProperty(ParentSubjectIdPropertyInfo, value);
      }
    }

    public static PropertyInfo<CslaSubjectEditCollection> SubSubjectsPropertyInfo = RegisterProperty(new PropertyInfo<CslaSubjectEditCollection>("SubSubjects"));
    public CslaSubjectEditCollection SubSubjects 
    { 
      get
      {
        return GetProperty(SubSubjectsPropertyInfo);
      }
      private set
      {
        LoadProperty(SubSubjectsPropertyInfo, value);
      }
    }

    #endregion [ Business Properties ]

    #region [ Validation Rules ]
    #endregion [ Validation Rules ]

    #region [ Authorization Rules ]
    #endregion [ Authorization Rules ]
	
    #region [ Factory Methods ]

    #region [ Root Factory Methods ]
    public static CslaSubjectEdit New()
    {
      return DataPortal.Create<CslaSubjectEdit>();
    }

    public static void Delete(SubjectKey key)
    {
      DataPortal.Delete<Subject>(new SingleCriteria<Subject, SubjectKey>(key));
    }

    public static void Delete(Guid SubjectId)
    {
      Delete(new SubjectKey { SubjectId = SubjectId });
    }
    #endregion [ Root Factory Methods ]

    #region [ Child Factory Methods ]
    internal static CslaSubjectEdit NewChild()
    {
      return DataPortal.CreateChild<CslaSubjectEdit>();
    }

    internal static CslaSubjectEdit GetSubjectEdit(Subject data, SubjectLoadSettings loadSettings)
    {
      if (data == null) throw new ArgumentNullException("data");
      if (loadSettings == null) throw new ArgumentNullException("loadSettings");
      CslaSubjectEdit result = new CslaSubjectEdit();
      result.FetchSubject(data, loadSettings);
      result.MarkAsChild();
      return result;
    }
    #endregion [ Child Factory Methods ]

    private static int ForceInitField = 0;
    protected CslaSubjectEdit() 
    {
      ForceInitField = 1;
    }
    #endregion [ Factory Methods ]

    #region [ Data Access ]

    #region [ Root Data Access ]
    protected override void DataPortal_Create()
    {
      base.DataPortal_Create();
    }

    [Transactional(TransactionalTypes.TransactionScope)]
    protected override void DataPortal_Insert()
    {
    }
    
    [Transactional(TransactionalTypes.TransactionScope)]
    protected override void DataPortal_Update()
    {
    }
    
    [Transactional(TransactionalTypes.TransactionScope)]
    protected override void DataPortal_DeleteSelf()
    {
      DataPortal_Delete(new SingleCriteria<Subject, SubjectKey>(new SubjectKey { SubjectId = this.SubjectId }));
    }
    
    [Transactional(TransactionalTypes.TransactionScope)]
    private void DataPortal_Delete(SingleCriteria<Subject, SubjectKey> criteria)
    {
    }
    #endregion [ Root Data Access ]

    #region [ Child Data Access ]
    protected override void Child_Create()
    {
      base.Child_Create();
    }
    
    private void Child_Fetch(object childData)
    {
    }
    
    private void Child_Insert(object parent)
    {
    }
    
    private void Child_Update(object parent)
    {
    }
    
    private void Child_DeleteSelf(object parent)
    {
    }
    #endregion [ Child Data Access ]

    protected void FetchSubject(Subject data, SubjectLoadSettings loadSettings)
    {
      FetchSubjectData(data, loadSettings);
      FetchSubjectChildren(data, loadSettings);
    }

    protected void FetchSubjectData(Subject data, SubjectLoadSettings loadSettings)
    {
      this.SubjectId = data.SubjectId;
      this.Url = data.Url;
      this.Title = data.Title;
      this.Description = data.Description;
      this.ImageUrl = data.ImageUrl;
      this.CreateDate = data.CreateDate;
      this.ModifyDate = data.ModifyDate;
      this.IsDisabled = data.IsDisabled;
      this.CreatorId = data.CreatorId;
      this.ParentSubjectId = data.ParentSubjectId;
    }

    protected void FetchSubjectChildren(Subject data, SubjectLoadSettings loadSettings)
    {
      if (loadSettings.Inclusion.IncludeCreator)
        this.Creator = CslaUserEdit.GetUserEdit(data.Creator, loadSettings.Inclusion.IncludedCreatorSettings);
      if (loadSettings.Inclusion.IncludeEditors)
        this.Editors = CslaUserEditCollection.GetUserEditCollection(data.Editors, loadSettings.Inclusion.IncludedEditorsSettings);
      if (loadSettings.Inclusion.IncludeParentSubject)
        this.ParentSubject = CslaSubjectEdit.GetSubjectEdit(data.ParentSubject, loadSettings.Inclusion.IncludedParentSubjectSettings);
      if (loadSettings.Inclusion.IncludeSubSubjects)
        this.SubSubjects = CslaSubjectEditCollection.GetSubjectEditCollection(data.SubSubjects, loadSettings.Inclusion.IncludedSubSubjectsSettings);
    }
    #endregion [ Data Access ]

    #region [ Explicit ISubject implementation ]

    Guid ISubject.SubjectId
    {
      get { return this.SubjectId; }
    }

    string ISubject.Url
    {
      get { return this.Url; }
      set
      {
        this.Url = value;
      }
    }

    string ISubject.Title
    {
      get { return this.Title; }
      set
      {
        this.Title = value;
      }
    }

    string ISubject.Description
    {
      get { return this.Description; }
      set
      {
        this.Description = value;
      }
    }

    string ISubject.ImageUrl
    {
      get { return this.ImageUrl; }
      set
      {
        this.ImageUrl = value;
      }
    }

    DateTime ISubject.CreateDate
    {
      get { return this.CreateDate; }
      set
      {
        this.CreateDate = value;
      }
    }

    DateTime ISubject.ModifyDate
    {
      get { return this.ModifyDate; }
      set
      {
        this.ModifyDate = value;
      }
    }

    bool ISubject.IsDisabled
    {
      get { return this.IsDisabled; }
      set
      {
        this.IsDisabled = value;
      }
    }

    IUser ISubject.Creator
    { 
      get
      {
        return this.Creator;
      }
      set
      {
        this.Creator = (CslaUserEdit)value;
      }
    }

    IBusinessObjectCollection<IUser> ISubject.Editors
    { 
      get
      {
        return this.Editors;
      }
    }

    ISubject ISubject.ParentSubject
    { 
      get
      {
        return this.ParentSubject;
      }
      set
      {
        this.ParentSubject = (CslaSubjectEdit)value;
      }
    }

    IBusinessObjectCollection<ISubject> ISubject.SubSubjects
    { 
      get
      {
        return this.SubSubjects;
      }
    }


    bool IBusinessObject.IsReadonly
    {
      get { return false; }
    }

    bool IBusinessObject.IsDirty
    {
      get { return this.IsDirty; }
    }

    #endregion [ Explicit ISubject implementation ]
  }
}
